package com.example.myapplication;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication.db.room.dao.MyAppDatabase;
import com.example.myapplication.db.room.entity.CalculationParameters;
import com.example.myapplication.history.HistoryAdapter;
import com.example.myapplication.history.HistoryData;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class HistoryActivity extends AppCompatActivity {
    @SuppressLint({"MissingInflatedId", "SetTextI18n"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        MyAppDatabase db = MyAppDatabase.getInstance(this);
        List<HistoryData> historyList = new ArrayList<>();
        List<CalculationParameters> all = db.calculationParametersDao().getAll();
        if (all != null) {
            for (CalculationParameters calculationParameters : all) {
                historyList.add(
                        new HistoryData(
                                formatTime(calculationParameters.getStartTime()),
                                formatTime(calculationParameters.getEndTime()),
                                calculationParameters.getAmount().toString() +" 元",
                                calculationParameters.getIntervalMinutes() +" 分钟") );


//                new HistoryData(
//                        formatTime(calculationParameters.getStartTime()),
//                        formatTime(calculationParameters.getEndTime()),
//                        calculationParameters.getAmount().doubleValue(),
//                        calculationParameters.getIntervalTime()));
            }
        }



//        historyList.add(new HistoryData("2024-04-28", "2024-04-29", 100.0, 3600));
//        historyList.add(new HistoryData("2024-04-28", "2024-04-29", 100.0, 3600));
//        historyList.add(new HistoryData("2024-04-28", "2024-04-29", 100.0, 3600));
//        historyList.add(new HistoryData("2024-04-28", "2024-04-29", 100.0, 3600));
//        historyList.add(new HistoryData("2024-04-28", "2024-04-29", 100.0, 3600));
//        historyList.add(new HistoryData("2024-04-28", "2024-04-29", 100.0, 3600));
//        historyList.add(new HistoryData("2024-04-28", "2024-04-29", 100.0, 3600));
//        historyList.add(new HistoryData("2024-04-28", "2024-04-29", 100.0, 3600));
//        historyList.add(new HistoryData("2024-04-28", "2024-04-29", 100.0, 3600));
//        historyList.add(new HistoryData("2024-04-28", "2024-04-29", 100.0, 3600));
//        historyList.add(new HistoryData("2024-04-28", "2024-04-29", 100.0, 3600));
//        historyList.add(new HistoryData("2024-04-28", "2024-04-29", 100.0, 3600));
        // 添加更多历史数据...

        ListView historyListView = findViewById(R.id.historyListView);
        HistoryAdapter adapter = new HistoryAdapter(this, historyList);
        historyListView.setAdapter(adapter);

        // 设置长按事件监听器...

//        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, historyList);
//        historyListView.setAdapter(adapter);

        historyListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                // 长按事件处理
                showPopupMenu(view);
                return true;
            }
        });
    }

    private void showPopupMenu(View view) {
        PopupMenu popupMenu = new PopupMenu(this, view);
        popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());

        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            int copyMenuItem =  R.id.copyMenuItem;
            int deleteMenuItem =  R.id.deleteMenuItem;
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                // 处理菜单项点击事件
                // 处理菜单项点击事件
                if (item.getItemId() == copyMenuItem) {
                    // 复制操作
                    Toast.makeText(HistoryActivity.this, "已复制", Toast.LENGTH_SHORT).show();
                    return true;
                } else if (item.getItemId() == deleteMenuItem) {
                    // 删除操作
                    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                    // historyList.remove(info.position);
                    // adapter.notifyDataSetChanged();
                    Toast.makeText(HistoryActivity.this, "已删除", Toast.LENGTH_SHORT).show();
                    return true;
                } else {
                    return false;
                }
            }
        });
        popupMenu.show();
    }
//    时间格式化
    public static String formatTime(Date time) {
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(time);
    }
}